package com.yikekong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import com.yikekong.entity.AdminEntity;
import com.yikekong.mapper.AdminMapper;
import com.yikekong.service.AdminService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper,AdminEntity> implements AdminService{
    @Override
    public Integer login(String loginName, String password) {
        //System.out.println(loginName+"   "+password);
        if(Strings.isNullOrEmpty(loginName) || Strings.isNullOrEmpty(password)){
            return -1;
        }
        //mybatis的条件构造器
        QueryWrapper<AdminEntity> queryWrapper = new QueryWrapper<>();

        queryWrapper
                .lambda()
                .eq(AdminEntity::getLoginName,loginName);   //where loginName = ''
        //System.out.println(queryWrapper);

        AdminEntity adminEntity = this.getOne(queryWrapper);   //获取一条记录
        if(adminEntity == null)
            return -1;

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        //passwordEncoder.matches(明文,密文) 进行对比
        String passwordEncode = passwordEncoder.encode(adminEntity.getPassword());
        if(passwordEncoder.matches(password,passwordEncode)){
            return adminEntity.getId();
        }
        return -1;

    }
}
